//
//  PBCollectionViewCell.mm
//  PlatinumButler
//
//  Created by 高小松 on 14-4-1.
//  Copyright (c) 2014年 gxsoft. All rights reserved.
//

#import "PBCollectionViewCell.h"
#import "PBRect.h"

@interface PBCollectionViewCell ()

@property (nonatomic, retain) UIImageView * imgView;
@property (nonatomic, retain) UILabel * titleLabel;

@end

@implementation PBCollectionViewCell

#pragma mark - Memory Management

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        _imgView = [[UIImageView alloc] init];
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont systemFontOfSize:15];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:_imgView];
        [self.contentView addSubview:_titleLabel];
    }
    return self;
}

- (void)dealloc
{
    [_imgView release];
    [_titleLabel release];
    [super dealloc];
}

#pragma mark - View Management

- (void)layoutSubviews
{
    static CGFloat margin = 2;
    CGRect rect = self.contentView.bounds;
    CGFloat edgeImage = CGRectGetWidth(rect) - 2 * margin;
    CGRect rcImage = TopCenterRect(rect, edgeImage, edgeImage, 0);
    _imgView.frame = rcImage;
    DeflateRect(&rect, 0, edgeImage, 0, 0);
    _titleLabel.frame = rect;
}

#pragma mark - Components

- (void)setTitle:(NSString *)title
{
    _titleLabel.text = title;
    [self setNeedsDisplay];
}

- (void)setImage:(UIImage *)image
{
    _imgView.image = image;
    [self setNeedsDisplay];
}

@end
